package service.impl;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import service.interf.IIfwRechargeInfo;
import dao.interf.IIfwRechargeInfoDAO;
import dao.interf.IIfwSrouterMapDAO;
import dao.pojo.IfwRechargeInfo;
import dao.pojo.IfwSrouterMap;

public class IfwRechargeInfoImpl extends BaseDataServiceImpl implements
		IIfwRechargeInfo {

	private IIfwRechargeInfoDAO iIfwRechargeInfoDao;
	private IIfwSrouterMapDAO iIfwSrouterMapDao;

	public void setiIfwRechargeInfoDao(IIfwRechargeInfoDAO iIfwRechargeInfoDao) {
		this.iIfwRechargeInfoDao = iIfwRechargeInfoDao;
	}

	public void setiIfwSrouterMapDao(IIfwSrouterMapDAO iIfwSrouterMapDao) {
		this.iIfwSrouterMapDao = iIfwSrouterMapDao;
	}

	public String findByCardNum(String cardNum) throws Exception {

		List<IfwRechargeInfo> list = iIfwRechargeInfoDao.findByProperties(
				new String[] { "cardNum" }, new Object[] { cardNum });

		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		if (null != list) {
			JSONObject jobj;
			for (IfwRechargeInfo ifwRechargeInfo : list) {
				jobj = new JSONObject();
				String sn = ifwRechargeInfo.getSn();
				List<IfwSrouterMap> ifwSrouterMaps = iIfwSrouterMapDao
						.findByProperties(new String[] { "sn" },
								new Object[] { sn });
				if (null != ifwSrouterMaps && !ifwSrouterMaps.isEmpty()) {
					sn = ifwSrouterMaps.get(0).getFwsn();
				}
				jobj.put("sn", sn);
				jobj.put("rechargeHost", ifwRechargeInfo.getRechargeHost());
				jobj.put("rechargeAc", ifwRechargeInfo.getRechargeAc());
				jobj.put("rechargeTime", ifwRechargeInfo.getRechargeTime());
				jobj.put("time", ifwRechargeInfo.getTime());
				jobj.put("cardNum", ifwRechargeInfo.getCardNum());
				ja.add(jobj);
			}
		}
		jo.put("rows", ja);
		return jo.toString();
	}

}
